#include <gtest/gtest.h>
#include "../../common/file.h"

TEST(FileTest, OpenUnExistFile)
{
	asce::File file;
	EXPECT_TRUE(file.Open("unexist1.txt"));
	EXPECT_TRUE(file.valid());
}

TEST(FileTest, OpenExistFile)
{
	asce::File file;
	EXPECT_TRUE(file.Open("test.txt"));
	EXPECT_TRUE(file.valid());
}

TEST(FileTest, OpenFileMoreTimes)
{
	asce::File file;
	EXPECT_TRUE(file.Open("test.txt"));
	EXPECT_TRUE(file.valid());
	EXPECT_FALSE(file.Open("test.txt"));
	EXPECT_TRUE(file.valid());
}

TEST(FileTest, CloseInvalidFile)
{
	asce::File file;
	EXPECT_FALSE(file.Close());
}

TEST(FileTest, CloseValidFile)
{
	asce::File file;
	EXPECT_TRUE(file.Open("test.txt"));
	EXPECT_TRUE(file.Close());
	EXPECT_FALSE(file.Close());
}

TEST(FileTest, CopyFile)
{
	asce::File source_file;
	EXPECT_TRUE(source_file.Open("test.txt"));

	asce::File dest_file;
	EXPECT_TRUE(dest_file.Open("dst1.txt"));

	char buffer[11] = {0};
	while (true)
	{
		size_t res = source_file.Read(buffer, 10);
		if (-1 == res)
		{
			break;
		}

		if (res < 10)
		{
			break;
		}

		res = dest_file.Write(buffer, res);
		if (-1 == res)
		{
			break;
		}
	}
}
